﻿using System;
using System.Windows.Forms;
using System.Data;
using Gencode.PrsCommon.Data.Dienst;
using System.Drawing;

namespace Gencode.PrsUtil.Controls
{
	public class PUDiensteComboBox : ComboBox
	{
		DataView m_dienste;
		string m_mandid;

		public PUDiensteComboBox()
			: base()
		{
			InitializeComponent();
		}

		protected override void InitLayout()
		{
			base.InitLayout();
		}

		public void InitData(string mandid)
		{
			m_mandid = mandid;
			RefreshData();
		}

		public void RefreshData()
		{
			m_dienste = DienstData.GetDienste(m_mandid, false, true);
			DataSource = m_dienste;
			DisplayMember = "Name";
		}

		protected override void Dispose(bool disposing)
		{
			if (m_dienste != null)
				m_dienste.Dispose();
			base.Dispose(disposing);
		}

		public string Mandid
		{
			private get
			{
				return m_mandid;
			}
			set
			{
				m_mandid = value;
			}
		}

		public int SelectedDienstID
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["DienstID"].ToString() != null &&
					m_dienste[SelectedIndex].Row["DienstID"].ToString() != string.Empty)
					return int.Parse(m_dienste[SelectedIndex].Row["DienstID"].ToString());

				return 0;
			}
		}

		public string SelectedDienstName
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["Name"].ToString() != null)
					return m_dienste[SelectedIndex].Row["Name"].ToString();

				return "";
			}
		}

		public string SelectedDienstKurztext
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["Kurztext"].ToString() != null)
					return m_dienste[SelectedIndex].Row["Kurztext"].ToString();

				return "";
			}
		}

		public string SelectedDienstLangtext
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["Langtext"].ToString() != null)
					return m_dienste[SelectedIndex].Row["Langtext"].ToString();

				return "";
			}
		}

		public bool SelectedDienstGanzerTag
		{
			get
			{
				int tmp = 0;
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["GanzerTag"].ToString() != null &&
					m_dienste[SelectedIndex].Row["GanzerTag"].ToString() != string.Empty)
				{
					tmp = int.Parse(m_dienste[SelectedIndex].Row["GanzerTag"].ToString());
					if (tmp == 0)
						return false;
					else if (tmp == 1)
						return true;
				}
				return false;
			}
		}

		public int SelectedDienstARGB
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["RGB"].ToString() != null &&
					m_dienste[SelectedIndex].Row["RGB"].ToString() != string.Empty)
					return int.Parse(m_dienste[SelectedIndex].Row["RGB"].ToString());

				return 0;
			}
		}

		public bool SelectedDienstGerechnet
		{
			get
			{
				int tmp = 0;
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["Gerechnet"].ToString() != null &&
					m_dienste[SelectedIndex].Row["Gerechnet"].ToString() != string.Empty)
				{
					tmp = int.Parse(m_dienste[SelectedIndex].Row["Gerechnet"].ToString());
					if (tmp == 0)
						return false;
					else if (tmp == 1)
						return true;
				}
				return false;
			}
		}

		public int SelectedDienstTyp
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["Typ"].ToString() != null &&
					m_dienste[SelectedIndex].Row["Typ"].ToString() != string.Empty)
					return int.Parse(m_dienste[SelectedIndex].Row["Typ"].ToString());

				return 0;
			}
		}

		public void SetIndexAt(int dienstid)
		{
			for (int i = 0; i < m_dienste.Count; i++)
			{
				if (m_dienste[i].Row["DienstID"].ToString() != string.Empty)
				{
					if (int.Parse(m_dienste[i].Row["DienstID"].ToString()) == dienstid)
					{
						SelectedIndex = i;
						i = m_dienste.Count;
					}
				}
			}
		}

		private void InitializeComponent()
		{
			this.SuspendLayout();
			// 
			// PUDiensteComboBox
			// 
			this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.PUDiensteComboBox_DrawItem);
			this.ResumeLayout(false);

		}

		private void PUDiensteComboBox_DrawItem(object sender, DrawItemEventArgs e)
		{
			// Get the Graphics Object (aka. CDC or Device Context Object )
			// passed via the DrawItemEventArgs parameter
			Graphics g = e.Graphics;

			// Get the bounding rectangle of the item currently being painted
			Rectangle r = e.Bounds;

			if (e.Index >= 0)
			{
				Rectangle rd = r;
				rd.Width = 40;

				Rectangle rt = new Rectangle(rd.Right, r.Top, r.Width - rd.Width, r.Height);
				//r.Left = rd.Right ; 

				// Get the brush object, at the specifid index in the colorArray
				Color c;
				if (m_dienste[e.Index].Row["RGB"].ToString() == "0")
					c = Color.White;
				else
				{
					try
					{
						c = Color.FromArgb(int.Parse(m_dienste[e.Index].Row["RGB"].ToString()));
					}
					catch
					{
						c = Color.White;
					}
				}
				SolidBrush b = new SolidBrush(c);

				// Set the string format options
				StringFormat sf = new StringFormat();
				sf.Alignment = StringAlignment.Near;

				// Fill a portion of the rectangle with the selected brush
				g.FillRectangle(b, rd);
				//e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2), rd);
				e.Graphics.DrawString(m_dienste[e.Index].Row["Name"].ToString(), this.Font, new SolidBrush(Color.Black), rd, sf);



				// Draw the rectangle
				//e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2), rt);

				if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
				{
					// if the item is not selected draw it with a different color
					e.Graphics.FillRectangle(new SolidBrush(Color.White), rt);
					e.Graphics.DrawString(m_dienste[e.Index].Row["Kurztext"].ToString(), this.Font, new SolidBrush(Color.Black), rt, sf);
					e.DrawFocusRectangle();
				}
				else
				{
					// if the item is selected draw it with a different color
					e.Graphics.FillRectangle(new SolidBrush(Color.DarkBlue), rt);
					e.Graphics.DrawString(m_dienste[e.Index].Row["Kurztext"].ToString(), this.Font, new SolidBrush(Color.White), rt, sf);
					e.DrawFocusRectangle();
				}

				//if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
				//{
				//    // if the item is not selected draw it with a different color
				//    e.Graphics.FillRectangle(new SolidBrush(Color.White), rt);
				//    e.Graphics.DrawString(b.Color.Name, new Font("Ariel", 8, FontStyle.Bold), new SolidBrush(Color.Black), rt, sf);
				//    e.DrawFocusRectangle();
				//}
				//else
				//{
				//    // if the item is selected draw it with a different color
				//    e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), rt);
				//    e.Graphics.DrawString(b.Color.Name, new Font("Veranda", 12, FontStyle.Bold), new SolidBrush(Color.Red), rt, sf);
				//    e.DrawFocusRectangle();
				//}
			}
		}
	}

	public class PUDienstTypComboBox : ComboBox
	{
		DataView m_diensttyp;
		string m_mandid;

		public PUDienstTypComboBox()
			: base()
		{
		}

		protected override void InitLayout()
		{
			base.InitLayout();
		}

		public void InitData(string mandid)
		{
			m_mandid = mandid;
			RefreshData();
		}

		public void RefreshData()
		{
			m_diensttyp = DienstData.GetDienstTyp(m_mandid, false);
			DataSource = m_diensttyp;
			DisplayMember = "textwert";
		}

		protected override void Dispose(bool disposing)
		{
			if (m_diensttyp != null)
				m_diensttyp.Dispose();
			base.Dispose(disposing);
		}

		public string Mandid
		{
			private get
			{
				return m_mandid;
			}
			set
			{
				m_mandid = value;
			}
		}

		public void SetIndexAt(int typid)
		{
			if (m_diensttyp != null)
			{
				for (int i = 0; i < m_diensttyp.Count; i++)
				{
					if (m_diensttyp[i].Row["wert"].ToString() != null &&
						m_diensttyp[i].Row["wert"].ToString() != string.Empty)
						if (int.Parse(m_diensttyp[i].Row["wert"].ToString()) == typid)
						{
							SelectedIndex = i;
							i = m_diensttyp.Count;
						}
				}
			}
		}

		public int SelectedDienstTyp
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_diensttyp[SelectedIndex].Row["wert"].ToString() != null &&
					m_diensttyp[SelectedIndex].Row["wert"].ToString() != string.Empty)
					return int.Parse(m_diensttyp[SelectedIndex].Row["wert"].ToString());

				return 0;
			}
		}
	}

	public class PUDienstSerieComboBox : ComboBox
	{
		DataView m_dienste;
		string m_mandid;

		public PUDienstSerieComboBox()
			: base()
		{
		}

		protected override void InitLayout()
		{
			base.InitLayout();
		}

		public void InitData(string mandid)
		{
			m_mandid = mandid;
			RefreshData();
		}

		public void RefreshData()
		{
			m_dienste = DienstData.GetDienstSerien(m_mandid, true);
			DataSource = m_dienste;
			DisplayMember = "Name";
		}

		protected override void Dispose(bool disposing)
		{
			if (m_dienste != null)
				m_dienste.Dispose();
			base.Dispose(disposing);
		}

		public string Mandid
		{
			private get
			{
				return m_mandid;
			}
			set
			{
				m_mandid = value;
			}
		}

		public int SelectedDienstID
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["DienstID"].ToString() != null &&
					m_dienste[SelectedIndex].Row["DienstID"].ToString() != string.Empty)
					return int.Parse(m_dienste[SelectedIndex].Row["DienstID"].ToString());

				return 0;
			}
		}

		public int SelectedPrsID
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["PrsID"].ToString() != null &&
					m_dienste[SelectedIndex].Row["PrsID"].ToString() != string.Empty)
					return int.Parse(m_dienste[SelectedIndex].Row["PrsID"].ToString());

				return 0;
			}
		}

		public int SelectedAbteilungID
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["AbteilungID"].ToString() != null &&
					m_dienste[SelectedIndex].Row["AbteilungID"].ToString() != string.Empty)
					return int.Parse(m_dienste[SelectedIndex].Row["AbteilungID"].ToString());

				return 0;
			}
		}

		public int SelectedTag
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["Tag"].ToString() != null &&
					m_dienste[SelectedIndex].Row["Tag"].ToString() != string.Empty)
					return int.Parse(m_dienste[SelectedIndex].Row["Tag"].ToString());

				return 0;
			}
		}

		public string SelectedTyp
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["Typ"].ToString() != null )
					return m_dienste[SelectedIndex].Row["Typ"].ToString();

				return "";
			}
		}

		public string SelectedVon
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["Von"].ToString() != null &&
					m_dienste[SelectedIndex].Row["Von"].ToString() != string.Empty)
					return Gencode.PrsCommon.App.DateUtil.ConvertSqlToDate(m_dienste[SelectedIndex].Row["Von"].ToString());

				return DateTime.Now.ToShortDateString();
			}
		}

		public string SelectedBis
		{
			get
			{
				if (SelectedIndex >= 0 &&
					m_dienste[SelectedIndex].Row["Bis"].ToString() != null &&
					m_dienste[SelectedIndex].Row["Bis"].ToString() != string.Empty)
					return Gencode.PrsCommon.App.DateUtil.ConvertSqlToDate(m_dienste[SelectedIndex].Row["Bis"].ToString());

				return DateTime.Now.ToShortDateString();
			}
		}
	}
}
